package com.ica.android.ui.policy;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import com.ica.R;
import com.ica.android.ui.policy.time.TimeTableManagerActivity;
import com.ica.model.command.AbstractCommand;
import com.ica.model.contact.ContactGroup;
import com.ica.model.facade.CommandFacadeMockImpl;
import com.ica.model.facade.ContactFacadeMockImpl;
import com.ica.model.facade.PolicyFacadeMockImpl;
import com.ica.model.policy.Policy;
import com.ica.model.policy.time.TimeStamp;
import com.ica.model.policy.time.utils.TimeUtils.DAYS;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author     Hector
 * @uml.dependency   supplier="com.ica.android.ui.policy.PolicyManagementActivity"
 */
public abstract class PolicyActivity extends Activity {
	
	/**
	 * @uml.property  name="policy"
	 * @uml.associationEnd  
	 */
	protected Policy policy = null;
	/**
	 */
	protected EditText etName = null;
	/**
	 * @uml.property  name="policyFacade"
	 * @uml.associationEnd  
	 */
	protected final PolicyFacadeMockImpl policyFacade = PolicyFacadeMockImpl.getInstance();
	/**
	 * @uml.property  name="commandFacade"
	 * @uml.associationEnd  
	 */
	protected final CommandFacadeMockImpl commandFacade = CommandFacadeMockImpl.getInstance();
	/**
	 * @uml.property  name="contactFacade"
	 * @uml.associationEnd  
	 */
	protected final ContactFacadeMockImpl contactFacade = ContactFacadeMockImpl.getInstance();
	/**
	 * @uml.property  name="contacts"
	 * @uml.associationEnd  multiplicity="(0 -1)"
	 */
	protected ContactGroup[] contacts;
	/**
	 * @uml.property  name="commands"
	 * @uml.associationEnd  multiplicity="(0 -1)"
	 */
	protected AbstractCommand[] commands;
	/**
	 */
	protected int lastCommand=-1;
	/**
	 */
	protected boolean[] checkedContacts;
	/**
	 * @uml.property  name="command"
	 * @uml.associationEnd  
	 */
	protected AbstractCommand command = null;
	/**
	 */
	protected HashMap<DAYS,List<TimeStamp>> timeFilter = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		TimeTableManagerActivity.clearResult();
	}
	
	protected void init(int layout) {		
		setContentView(layout);
        Button save = (Button) findViewById(R.id.bSavePolicy);
        etName = (EditText) findViewById(R.id.etPolicyName);
        save.setOnClickListener(onClickSave());
        final Collection<ContactGroup> c = ContactFacadeMockImpl.getInstance().getElements();
        checkedContacts = new boolean[c.size()];
        contacts = (ContactGroup[]) c.toArray(new ContactGroup[c.size()]);
		final String[] contactGroupNames = new String[contacts.length];
		for(int i=0;i<contacts.length;i++)
			contactGroupNames[i]=contacts[i].getName();
        final Collection<AbstractCommand> abs = CommandFacadeMockImpl.getInstance().getElements();
        commands = (AbstractCommand[]) abs.toArray(new AbstractCommand[abs.size()]);
        final String[] commandNames = new String[commands.length];
		for(int i=0;i<commands.length;i++)
			commandNames[i]=commands[i].getName();   
        Button contactGroup = (Button) findViewById(R.id.bAddContactGroup);
        contactGroup.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {				
				AlertDialog.Builder builder = new AlertDialog.Builder(PolicyActivity.this);
				builder.setTitle("Pick a Contact Group");
				builder.setMultiChoiceItems(contactGroupNames, checkedContacts, 
					new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton, boolean isChecked) {
                        checkedContacts[whichButton] = isChecked;
                    }
				})
                .setPositiveButton(R.string.save_name, null);
				AlertDialog dialog = builder.create();
				for(int i=0;i<checkedContacts.length;i++)
					dialog.getListView().setItemChecked(i, checkedContacts[i]);
				dialog.show();	
			}
		});
        Button bCommand = (Button) findViewById(R.id.bAddCommand);
        bCommand.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {				
				AlertDialog.Builder builder = new AlertDialog.Builder(PolicyActivity.this);
				builder.setTitle("Pick a Command");
				builder.setSingleChoiceItems(commandNames, 
					lastCommand, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {                       
                    	command = commands[whichButton];
                    	lastCommand=whichButton;
                    }

				})
				.setPositiveButton(R.string.save_name, null);
				builder.create().show();
			}
		});
        Button bTimeFilter = (Button) findViewById(R.id.bAddTimeFilter);
        bTimeFilter.setOnClickListener(onClickTimeTable());
	}

	protected abstract OnClickListener onClickSave();
	protected abstract OnClickListener onClickTimeTable();
}
